--trade runes
--the price that buy or sell one rune need,must be integer
local rune_buy_price = 500000
local rune_sell_price = 10000

local rune_selected = 0
local success = false

local rune_might_count_new = 0
local rune_mind_count_new = 0
local rune_magic_count_new = 0
local gold_count_new = 0

function get_rune_trade_count()
	local rune_might = get_rune_count_might()
	local rune_mind = get_rune_count_mind()
	local rune_magic = get_rune_count_magic()
	return "<align=center><label=rune_might><image=Quests_icon_rune_might.png>=<color=225,0,0>"..rune_might.."</color><br><label=rune_mind><image=Quests_icon_rune_mind.png>=<color=225,0,0>"..rune_mind.."</color><br><label=rune_magic><image=Quests_icon_rune_magic.png>=<color=225,0,0>"..rune_magic.."</color>"
end

function get_rune_trade_choice()

	local rune_type,rune_img = get_rune_trade_type()
    if rune_selected == 1 or rune_selected ==2 or rune_selected ==3 then
      return "<label=trade_buy>".."<image=Quests_icon_money.png>"..tonumber(rune_buy_price).."<label=buy>".."<label=num1>"..rune_img..rune_type
    elseif rune_selected == 4 or rune_selected ==5 or rune_selected ==6 then
     return "<label=trade_sell>".."<image=Quests_icon_money.png>"..tonumber(rune_sell_price).."<label=sell>".."<label=num1>"..rune_img..rune_type
	end
end

function get_rune_trade_type()
    if rune_selected == 1 or rune_selected == 4 then
      return "<label=rune_might>","<image=Quests_icon_rune_might.png>"
    elseif rune_selected == 2 or rune_selected == 5 then
      return "<label=rune_mind>","<image=Quests_icon_rune_mind.png>"
	elseif rune_selected == 3 or rune_selected == 6 then
      return "<label=rune_magic>","<image=Quests_icon_rune_magic.png>"
	end
end

function get_rune_buy_exit()
	if get_gold_count() < rune_buy_price then
		return "<label=money_not_enough>"
	else
	    return "<label=dlg_exit>"
	end
end

function get_rune_sell_exit()
	if get_rune_count_total() == 0 then
		return "<label=rune_not_enough>"
	else
	    return "<label=dlg_exit>"
	end
end

function get_rune_buy_price()
	local label = tostring(rune_buy_price)
	return label
end

function get_rune_sell_price()
	local label = tostring(rune_sell_price)
	return label
end

------------------------------
-- trade the selected runes
------------------------------
function rune_trade_chosen_runes(choice_number)
    success=false
	-- Sanity check
	if choice_number >= 1 and choice_number <= 6 then
		-- Get the number of runes
		local rune_might_count = get_rune_count_might()
		local rune_mind_count = get_rune_count_mind()
		local rune_magic_count = get_rune_count_magic()
		local gold_count = get_gold_count()
		-- Init new amount of runes
		rune_might_count_new = rune_might_count
		rune_mind_count_new = rune_mind_count
		rune_magic_count_new = rune_magic_count
		gold_count_new = gold_count

		-- Check what the user's choice was
		if choice_number == 1 and gold_count>=rune_buy_price then
			-- buy Might runes
			rune_might_count_new = rune_might_count + 1
			gold_count_new = gold_count - rune_buy_price
			success=true
		elseif choice_number == 2 and gold_count>=rune_buy_price then
			-- buy Mind runes
			rune_mind_count_new = rune_mind_count + 1
			gold_count_new = gold_count - rune_buy_price
			success=true
		elseif choice_number == 3 and gold_count>=rune_buy_price then
			-- buy Magic runes
			rune_magic_count_new = rune_magic_count + 1
			gold_count_new = gold_count - rune_buy_price
			success=true
		elseif choice_number == 4 and rune_might_count >= 1 then
			-- sell Might runes
			rune_might_count_new = rune_might_count - 1
			gold_count_new = gold_count + rune_sell_price
			success=true
		elseif choice_number == 5 and rune_mind_count >= 1 then
			-- sell Mind runes
			rune_mind_count_new = rune_mind_count - 1
			gold_count_new = gold_count + rune_sell_price
			success=true
		elseif choice_number == 6 and rune_magic_count >= 1 then
			-- sell Magic runes
			rune_magic_count_new = rune_magic_count - 1
			gold_count_new = gold_count + rune_sell_price
			success=true
		end
	end
end

-----------------------------------------------
-- Conditions for displaying the dialog options
-----------------------------------------------
function rune_trade_dlg_condition_n(n)
	local condition=false
	Logic.hero_lu_var("rune_trade_dlg",0)
	if n==1 or n==2 or n==3 then
		if get_gold_count() >= rune_buy_price then
			condition=true
		end
	elseif n==4 then
		if get_rune_count_might() >= 1 then
			condition=true
		end
	elseif n==5 then
	    if get_rune_count_mind() >= 1 then
		   condition=true
	    end
	elseif n==6 then
	    if get_rune_count_magic() >= 1 then
		   condition = true
		end
	end
    if condition then
	   Logic.hero_lu_var("rune_trade_dlg",n)
	end
	return condition
end


function rune_buy_dlg_condition1(param) --ftag:if
	return rune_trade_dlg_condition_n(1)
end

function rune_buy_dlg_condition2(param) --ftag:if
	return rune_trade_dlg_condition_n(2)
end

function rune_buy_dlg_condition3(param) --ftag:if
	return rune_trade_dlg_condition_n(3)
end

function rune_sell_dlg_condition1(param) --ftag:if
	return rune_trade_dlg_condition_n(4)
end

function rune_sell_dlg_condition2(param) --ftag:if
	return rune_trade_dlg_condition_n(5)
end

function rune_sell_dlg_condition3(param) --ftag:if
	return rune_trade_dlg_condition_n(6)
end

--------------------------------------------------------
-- Store the user's choice in the dialog and force usage
--------------------------------------------------------
function rune_trade_dlg_use_x(x)
	rune_trade_chosen_runes(tonumber(x))
	return true
end

function rune_buy_dlg_use1(param) --ftag:action
    rune_selected = 1
	return rune_trade_dlg_use_x(1)
end

function rune_buy_dlg_use2(param) --ftag:action
    rune_selected = 2
	return rune_trade_dlg_use_x(2)
end

function rune_buy_dlg_use3(param) --ftag:action
    rune_selected = 3
	return rune_trade_dlg_use_x(3)
end

function rune_sell_dlg_use1(param) --ftag:action
    rune_selected = 4
	return rune_trade_dlg_use_x(4)
end

function rune_sell_dlg_use2(param) --ftag:action
    rune_selected = 5
	return rune_trade_dlg_use_x(5)
end

function rune_sell_dlg_use3(param) --ftag:action
    rune_selected = 6
	return rune_trade_dlg_use_x(6)
end

function rune_trade_dlg_choice(param)
	if success then
		Logic.hero_lu_item("rune_might", "count", rune_might_count_new)
		Logic.hero_lu_item("rune_mind", "count", rune_mind_count_new)
		Logic.hero_lu_item("rune_magic", "count", rune_magic_count_new)
		Logic.hero_lu_item("money","count",gold_count_new)
	end
	return true
end


